package day7;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

//实现: socket接收数据
public class Server1 {
    public static void main(String[] args) throws IOException {
        //1.创建对象
        ServerSocket server=new ServerSocket(9999);
        while(true){
            try {
                //2.等待客户端连接
                Socket client = server.accept();
                System.out.println(client + "连接成功");
                //3.获取输入输出流
                InputStream is = client.getInputStream();
                OutputStream os = client.getOutputStream();
                String message = "你连接成功了\n";
                os.write(message.getBytes());

                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String result = br.readLine();//bug 读取一行
                System.out.println(client.getInetAddress() + "客户端:" + result);
            }catch (Exception e){

            }
        }
    }
}
class Client1{
    public static void main(String[] args) throws IOException {
        //1.创建对象
        Socket client=new Socket("192.168.0.114",9999);
        //2.创建输入输出流
        BufferedReader br=new BufferedReader(
                new InputStreamReader(
                        client.getInputStream()));
        OutputStream os=client.getOutputStream();
        String result=br.readLine();
        System.out.println("服务端:"+result);
        os.write("我是客户端\n".getBytes());
    }
}
